﻿using Kingdee.BOS;
using Kingdee.BOS.WebApi.FormService;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace KingdeeWebServiceProject.Utils
{
    internal class CommonUtils
    {
        internal static JObject SaveMethod(Context context, string FormId, string jsonData)
        {
            JObject result = new JObject
            {
                { "code", 500 }
            };
            try
            {
                StringBuilder stringBuilder = new StringBuilder();//接收失败原因
                object saveResult = WebApiServiceCall.Save(context, FormId, jsonData);//调用保存方法  
                Dictionary<string, object> responseStatus = ((Dictionary<string, object>)((Dictionary<string, object>)saveResult)["Result"])["ResponseStatus"] as Dictionary<String, object>;
                JObject obj = JObject.FromObject(saveResult);
                JObject resultStr = JObject.Parse(obj["Result"].ToString());
                JObject ResponseStatus = JObject.Parse(resultStr["ResponseStatus"].ToString());
                if (ResponseStatus["IsSuccess"].ToString().ToUpper() == "TRUE")
                {
                    if (!ResponseStatus["SuccessEntitys"].ToString().Equals("[]"))
                    {
                        result["code"] = 200;
                        JArray content = JArray.Parse(ResponseStatus["SuccessEntitys"].ToString());
                        result["Number"] = content.First["Number"].ToString();
                    }
                    else
                    {
                        result.Add("msg", "保存失败！详情：未知错误");
                    }
                }
                else
                {
                    List<object> errors = responseStatus["Errors"] as List<object>;
                    for (int a = 0; a < errors.Count(); a++)
                    {
                        Dictionary<string, object> keyValues = errors[a] as Dictionary<string, object>;
                        stringBuilder.AppendLine("第" + keyValues["DIndex"] + "行分录:" + keyValues["Message"]);
                    }
                    result.Add("msg", "保存失败！详情：" + stringBuilder.ToString());
                }
            }
            catch (Exception ex)
            {
                result.Add("msg", "保存失败！详情：" + ex.Message);
            }
            return result;
        }

        internal static JObject SubmitMethod(Context context, string FormId, string jsonData)
        {
            JObject result = new JObject
            {
                { "code", 500 }
            };
            try
            {
                StringBuilder stringBuilder = new StringBuilder();//接收失败原因
                object submitResult = WebApiServiceCall.Submit(context, FormId, jsonData);//调用保存方法  
                Dictionary<string, object> responseStatus = ((Dictionary<string, object>)((Dictionary<string, object>)submitResult)["Result"])["ResponseStatus"] as Dictionary<String, object>;

                JObject obj = JObject.FromObject(submitResult);
                JObject resultStr = JObject.Parse(obj["Result"].ToString());
                JObject ResponseStatus = JObject.Parse(resultStr["ResponseStatus"].ToString());
                if (ResponseStatus["IsSuccess"].ToString().ToUpper() == "TRUE")
                {
                    result["code"] = 200;
                }
                else
                {
                    List<object> errors = responseStatus["Errors"] as List<object>;
                    for (int a = 0; a < errors.Count(); a++)
                    {
                        Dictionary<string, object> keyValues = errors[a] as Dictionary<string, object>;
                        stringBuilder.AppendLine("第" + keyValues["DIndex"] + "行分录:" + keyValues["Message"]);
                    }
                    result.Add("msg", "提交失败！详情：" + stringBuilder.ToString());
                }
            }
            catch (Exception ex)
            {
                result.Add("msg", "提交失败！详情：" + ex.Message);
            }
            return result;
        }

        internal static JObject AuditMethod(Context context, string FormId, string jsonData)
        {
            JObject result = new JObject
            {
                { "code", 500 }
            };
            try
            {
                StringBuilder stringBuilder = new StringBuilder();//接收失败原因
                object auditResult = WebApiServiceCall.Audit(context, FormId, jsonData);//调用方法  
                Dictionary<string, object> responseStatus = ((Dictionary<string, object>)((Dictionary<string, object>)auditResult)["Result"])["ResponseStatus"] as Dictionary<String, object>;

                JObject obj = JObject.FromObject(auditResult);
                JObject resultStr = JObject.Parse(obj["Result"].ToString());
                JObject ResponseStatus = JObject.Parse(resultStr["ResponseStatus"].ToString());
                if (ResponseStatus["IsSuccess"].ToString().ToUpper() == "TRUE")
                {
                    result["code"] = 200;
                }
                else
                {
                    List<object> errors = responseStatus["Errors"] as List<object>;
                    for (int a = 0; a < errors.Count(); a++)
                    {
                        Dictionary<string, object> keyValues = errors[a] as Dictionary<string, object>;
                        stringBuilder.AppendLine("第" + keyValues["DIndex"] + "行分录:" + keyValues["Message"]);
                    }
                    result.Add("msg", "审核失败！详情：" + stringBuilder.ToString());
                }
            }
            catch (Exception ex)
            {
                result.Add("msg", "审核失败！详情：" + ex.Message);
            }
            return result;
        }
    }
}
